﻿using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using Loki.Game;

namespace WindowPlacer
{
    [XmlRoot("Settings")]
    public class WindowInfo
    {
        [XmlIgnore]
        private string _defaultSettingsPath = string.Format(Loki.Utilities.Xml.XmlSettings.SettingsDirectory + "\\WindowPlacer\\");

        [XmlElement("ResizeAndReplacePoEWindow")]
        public bool ResizeAndReplacePoeWindow { get; set; }

        [XmlElement("PoeX")]
        public int PoeX { get; set; }

        [XmlElement("PoeY")]
        public int PoeY { get; set; }

        [XmlElement("PoeWidth")]
        public int PoeWidth { get; set; }

        [XmlElement("PoeHeight")]
        public int PoeHeight { get; set; }

        [XmlElement("BotX")]
        public int BotX { get; set; }

        [XmlElement("BotY")]
        public int BotY { get; set; }

        [XmlElement("BotWidth")]
        public int BotWidth { get; set; }

        [XmlElement("BotHeight")]
        public int BotHeight { get; set; }

        // Clean Constructor
        public WindowInfo()
        {
            
        }

        public WindowInfo(int myBotX, int myBotY, int myBotWidth, int myBotHeight, int myPoeX, int myPoeY, int myPoeWidth, int myPoeHeight)
        {
            // Bot Infos
            BotX = myBotX;
            BotY = myBotY;
            BotWidth = myBotWidth;
            BotHeight = myBotHeight;

            // Poe Infos
            PoeX = myPoeX;
            PoeY = myPoeY;
            PoeWidth = myPoeWidth;
            PoeHeight = myPoeHeight;
        }

        public void SaveInformations()
        {
            // Handling Namespaces
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            string path = _defaultSettingsPath +
                          string.Format("{0} - {1}.xml", LokiPoe.ObjectManager.Me.League, LokiPoe.ObjectManager.Me.Name);

            if (!Directory.Exists(_defaultSettingsPath))
                Directory.CreateDirectory(_defaultSettingsPath);

            var xs = new XmlSerializer(typeof(WindowInfo));
            using (var sw = new StreamWriter(path.Replace(@"\\", @"\")))
            {
                xs.Serialize(sw, this, ns);
            }  
        }

        public bool GetInformations()
        {
            string path = _defaultSettingsPath +
                          string.Format(@"{0} - {1}.xml", LokiPoe.ObjectManager.Me.League, LokiPoe.ObjectManager.Me.Name);

            if (!File.Exists(path)) return false;

            var xs = new XmlSerializer(typeof(WindowInfo));
            using (var rd = new StreamReader(path))
            {
                var mySettings = (WindowInfo)xs.Deserialize(rd);
                ResizeAndReplacePoeWindow = mySettings.ResizeAndReplacePoeWindow;

                PoeX = mySettings.PoeX;
                PoeY = mySettings.PoeY;
                PoeWidth = mySettings.PoeWidth;
                PoeHeight = mySettings.PoeHeight;

                BotX = mySettings.BotX;
                BotY = mySettings.BotY;
                BotWidth = mySettings.BotWidth;
                BotHeight = mySettings.BotHeight;
            }

            return true;
        }

        public void PlaceWindow()
        {
            var myBotProcessInfo = new ProcessStartInfo
            {
                WorkingDirectory = @"D:\A_ExileBuddy\RELEASE\Plugins\WindowPlacer\",
                FileName = "MoveExilebuddy.exe",
                Arguments = string.Format("{0} {1} {2} {3}", BotX, BotY, BotWidth, BotHeight)
            };

            Process.Start(myBotProcessInfo);

            if (ResizeAndReplacePoeWindow)
            {
                var myPoEProcessInfo = new ProcessStartInfo
                {
                    WorkingDirectory = @"D:\A_ExileBuddy\RELEASE\Plugins\WindowPlacer\",
                    FileName = "MovePoE.exe",
                    Arguments = string.Format("{0} {1} {2} {3}", PoeX, PoeY, PoeWidth, PoeHeight)
                };

                Process.Start(myPoEProcessInfo);
            }

        }
    }
}

